package com.kaichaohulian.baocms.activity_newcreate;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kaichaohulian.baocms.NearService;
import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.UserInfoManager;
import com.kaichaohulian.baocms.adapter.MyGridAdapter;
import com.kaichaohulian.baocms.app.MyApplication;
import com.kaichaohulian.baocms.ecdemo.ui.group.GroupService;
import com.kaichaohulian.baocms.fragment.DiscoverFragment;
import com.kaichaohulian.baocms.fragment.HomeFragment2;
import com.kaichaohulian.baocms.fragment.MeFragment;
import com.kaichaohulian.baocms.fragment.MoreFragment;
import com.yuntongxun.ecsdk.ECError;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.kaichaohulian.baocms.R.id.ib_contact_list2;

/**
 * 主界面
 * Created by ljl on 2016/12/11.
 */
@SuppressLint("ValidFragment")
public class NavigationActivity extends FragmentActivity implements GroupService.Callback {
    @BindView(R.id.content)
    FrameLayout content;
    @BindView(R.id.img_index)
    ImageView imgIndex;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.layout_home)
    LinearLayout layoutHome;
    @BindView(R.id.img_invest)
    ImageView imgInvest;
    @BindView(R.id.tv_invest)
    TextView tvInvest;
    @BindView(R.id.layout_invest)
    LinearLayout layoutInvest;
    @BindView(R.id.img_zichan)
    ImageView imgZichan;
    @BindView(R.id.tv_zichan)
    TextView tvZichan;
    @BindView(R.id.layout_zichan)
    LinearLayout layoutZichan;
    @BindView(R.id.img_more)
    ImageView imgMore;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.layout_more)
    LinearLayout layoutMore;
    @BindView(ib_contact_list2)
    ImageView ibContactList2;


    private HomeFragment2 homeFragment;
    //    private TouziFragment touziFragment;
    private DiscoverFragment touziFragment;
    private MeFragment meFragment;
    private MoreFragment moreFragment;
    private FragmentTransaction ft;
    private PopupWindow popWindow;
    //网格
    GridView gridview;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);
        myApplication = (MyApplication) getApplication();
        setSelect(0);
    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        reSetTab();
        hideFragment();
        switch (i) {
            case 0:
                //首页
                if (homeFragment == null) {
                    homeFragment = new HomeFragment2();
                    ft.add(R.id.content, homeFragment);
                }
//                ivHome.setImageResource(R.drawable.bid01);
                //  tvHome.setTextColor(UIUtils.getCol(R.color.home_back_selected));
                imgIndex.setImageResource(R.mipmap.xiaoxi_pressed);
                tvHome.setTextColor(getResources().getColor(R.color.black));
                ft.show(homeFragment);
                break;
            case 1:
                //发现
                if (touziFragment == null) {
//                    touziFragment = new TouziFragment();
                    touziFragment = new DiscoverFragment(myApplication, NavigationActivity.this, NavigationActivity.this);
                    ft.add(R.id.content, touziFragment);
                }
                startService(new Intent(NavigationActivity.this, NearService.class));
                imgInvest.setImageResource(R.mipmap.contact_list_pressed_mod);
                tvInvest.setTextColor(getResources().getColor(R.color.black));
                //  tvTouzi.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                ft.show(touziFragment);
//                startService(new Intent(NavigationActivity.this, NearService.class));
                break;
            case 2:
                //资产
                if (meFragment == null) {
                    meFragment = new MeFragment();
                    ft.add(R.id.content, meFragment);
                }
                imgZichan.setImageResource(R.mipmap.notice_selected_mod);
                tvZichan.setTextColor(getResources().getColor(R.color.black));
                // tvMe.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                ft.show(meFragment);
                break;
            case 3:
                //更多
                if (moreFragment == null) {
                    moreFragment = new MoreFragment();
                    ft.add(R.id.content, moreFragment);
                }
                imgMore.setImageResource(R.mipmap.profile_pressed_mod);
                tvMore.setTextColor(getResources().getColor(R.color.black));
                UserInfoManager.getInstance().updateUserCache(NavigationActivity.this);
                //  tvMore.setTextColor(UIUtils.getColor(R.color.home_back_selected));
                ft.show(moreFragment);
                break;
        }
        ft.commit();
    }


    private void reSetTab() {
        imgIndex.setImageResource(R.mipmap.xiaoxi_normal);
        imgInvest.setImageResource(R.mipmap.contact_list_normal_mod);
        imgZichan.setImageResource(R.mipmap.notice_selected_normal_mod);
        imgMore.setImageResource(R.mipmap.profile_normal_mod);
        tvInvest.setTextColor(getResources().getColor(R.color.home_back_unselected));
        tvMore.setTextColor(getResources().getColor(R.color.home_back_unselected));
        tvHome.setTextColor(getResources().getColor(R.color.home_back_unselected));
        tvZichan.setTextColor(getResources().getColor(R.color.home_back_unselected));
//        ivMe.setImageResource(R.drawable.bid06);
//        ivMore.setImageResource(R.drawable.bid08);

    }


    private void hideFragment() {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (touziFragment != null) {
            ft.hide(touziFragment);
        }
        if (meFragment != null) {
            ft.hide(meFragment);
        }
        if (moreFragment != null) {
            ft.hide(moreFragment);
        }
    }


    @OnClick({R.id.content, R.id.img_index, R.id.tv_home, R.id.layout_home, R.id.img_invest, R.id.tv_invest, R.id.layout_invest, R.id.img_zichan, R.id.tv_zichan, R.id.layout_zichan, R.id.img_more, R.id.tv_more, R.id.layout_more, ib_contact_list2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.content:
                break;
            case R.id.img_index:
                setSelect(0);
                break;
            case R.id.tv_home:
                setSelect(0);
                break;
            case R.id.layout_home:
                setSelect(0);
                break;
            case R.id.img_invest:
                setSelect(1);
                startService(new Intent(NavigationActivity.this, NearService.class));
                break;
            case R.id.tv_invest:
                setSelect(1);
                startService(new Intent(NavigationActivity.this, NearService.class));
                break;
            case R.id.layout_invest:
                setSelect(1);
                startService(new Intent(NavigationActivity.this, NearService.class));
                break;
            case R.id.img_zichan:
                setSelect(2);
                break;
            case R.id.tv_zichan:
                setSelect(2);
                break;
            case R.id.layout_zichan:
                setSelect(2);
                break;
            case R.id.img_more:
                setSelect(3);
                break;
            case R.id.tv_more:
                setSelect(3);
                break;
            case R.id.layout_more:
                setSelect(3);
                break;
            case R.id.ib_contact_list2:
                showPopupWindow(ibContactList2);
                break;
        }
    }


    private void showPopupWindow(View parent) {
        if (popWindow == null) {

            View view = LayoutInflater.from(this).inflate(R.layout.dialog_01, null);

            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop(View view) {
        TextView tv01 = (TextView) view.findViewById(R.id.dl_tv_01);//拍照
        TextView tv02 = (TextView) view.findViewById(R.id.dl_tv_02);//相册
        LinearLayout layout_dismiss = (LinearLayout) view.findViewById(R.id.layout_dismiss);
        gridview = (GridView) view.findViewById(R.id.gridview);
        gridview.setAdapter(new MyGridAdapter(this));

//        view2.getBackground().setAlpha(160);//0~255透明度值

        layout_dismiss.getBackground().setAlpha(230);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        startActivity(new Intent(NavigationActivity.this, HelpGetActivity.class));
                        popWindow.dismiss();
                        break;
                    case 1:
                        startActivity(new Intent(NavigationActivity.this, HelpPuraseActivity.class));
                        popWindow.dismiss();
                        break;

                    case 2:
                        startActivity(new Intent(NavigationActivity.this, HelpGiveActivity.class));
                        popWindow.dismiss();
                        break;
                    case 3:
                        startActivity(new Intent(NavigationActivity.this, PaiDuiActivity.class));
                        popWindow.dismiss();
                        break;
                    case 4:
                        startActivity(new Intent(NavigationActivity.this, YuYueActivity.class));
                        popWindow.dismiss();
                        break;
                    case 5:
                        startActivity(new Intent(NavigationActivity.this, YuYueActivity.class));
                        popWindow.dismiss();
                        break;
                    case 6:
                        startActivity(new Intent(NavigationActivity.this, YuYueActivity.class));
                        popWindow.dismiss();
                        break;
                    case 7:
                        startActivity(new Intent(NavigationActivity.this, YuYueActivity.class));
                        popWindow.dismiss();
                        break;
                    case 8:
                        startActivity(new Intent(NavigationActivity.this, YuYueActivity.class));
                        popWindow.dismiss();
                        break;
                    case 9:
                        startActivity(new Intent(NavigationActivity.this, YuYueActivity.class));
                        popWindow.dismiss();
                        break;
                }


            }
        });
        final ImageView img_close = (ImageView) view.findViewById(R.id.img_close);
        img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });
        layout_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dismiss();
            }
        });

        tv01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                popWindow.dismiss();

            }
        });
        tv02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {


                popWindow.dismiss();
            }
        });
    }

    @Override
    public void onSyncGroup() {

    }

    @Override
    public void onSyncGroupInfo(String groupId) {

    }

    @Override
    public void onGroupDel(String groupId) {

    }

    @Override
    public void onError(ECError error) {

    }

    @Override
    public void onUpdateGroupAnonymitySuccess(String groupId, boolean isAnonymity) {

    }
}
